#!/usr/bin/env python

import sys
import os
import pickle
from keeper import *

__author__   = "Bin Fan"
__version__  = "$Revision: 0.01 $"
__license__  = "Apache 2.0"

# $Source$

pbluedir = os.path.expanduser("~/Dropbox/pblue")
print "pblue root dir is %s"%(pbluedir)
metadata_path = os.path.join(pbluedir, "keeper")

if os.path.exists(metadata_path):
    with open(metadata_path, 'rb') as f:
        db = pickle.load(f)
else:
    db = Keeper(pbluedir)

# show usage information
def usage():
    print "pblue <subcommand> [args]"
    print "subcommand and args:"
    print "\tpush  [hashid | ailas]  : sync the remote repo to the working dir"
    print "\tpull  [hashid | alias]  : sync the working dir according to remote "
    print "\tdiff  [hashid | alias]  : compare working dir with remote"
    print "\tinfo  [dirname]"
    print "\tlist"
    print "\tversion               : show version"
    exit(0)

def exec_sync(src, dst):
    print "syncing src (%s) to dst (%s)"%(src, dst)
    cmd = "rsync -rv %s/ %s/"%(src, dst)
    print cmd
    os.system(cmd)

def exec_diff(src, dst):
    print "comparing src (%s) with dst (%s)"%(src, dst)
    cmd = "diff -rq %s/ %s/"%(src, dst)
    print cmd
    os.system(cmd)
    
def pblue_push(workingdir, alias = None, hashid = None):
    if hashid:
        repo = db.lookup_by_hashid(hashid)
    elif alias:
        repo = db.lookup_by_alias(alias)
    else:
        repo = db.lookup_by_workingdir(workingdir)

    if not repo:
        repo = db.register(workingdir)
        print "register new repo"
        print "\thashid = %s"%(repo.hashid)
        
    if alias and db.link_alias(alias, repo.hashid):
        print "\talias = %s"%(alias)
            
        

    src = workingdir
    dst = repo.path
    exec_sync(src, dst)


def pblue_pull(workingdir, alias = None, hashid = None):
    if hashid:
        repo = db.lookup_by_hashid(hashid)
    elif alias:
        repo = db.lookup_by_alias(alias)
    else:
        repo = db.lookup_by_workingdir(workingdir)
   
    if repo:
        src = repo.path
        dst = workingdir
        exec_sync(src, dst)


def pblue_diff(workingdir, alias = None, hashid = None):
    if hashid:
        repo = db.lookup_by_hashid(hashid)
    elif alias:
        repo = db.lookup_by_alias(alias)
    else:
        repo = db.lookup_by_workingdir(workingdir)
   
    if repo:
        src = repo.path
        dst = workingdir
        exec_diff(src, dst)


def pblue_info():
    print "not supported"

def pblue_list():
    print "listing all repositories"
    for repo in db._Keeper__map_hashid.values():
        print "hashid = %s"%(repo.hashid)
        for alias in repo.alias:
            print "alias  = %s"%(alias)
        for wd in repo.workingdir:
            print "workingdir = %s"%(wd)



def pblue_version():
    print version
    

if __name__ == "__main__":
    
    try:
        subcommand = sys.argv[1]
    except:
        print "please specify a subcommand"
        usage()

    hashid = None
    alias  = None
    if len(sys.argv) >= 3:
        arg = sys.argv[2]
        print "arg is", arg
        if len(arg) == 40:
            try:
                int(arg, 16)
            except:
                pass
            else:
                hashid = arg
        if hashid is None:
            alias = arg



            
    workingdir = "."
    # dispatch subcommand 
    if   subcommand == "push":
        pblue_push(workingdir, alias, hashid)
    elif subcommand == "pull":
        pblue_pull(workingdir, alias, hashid)
    elif subcommand == "diff":
        pblue_diff(workingdir, alias, hashid)
    elif subcommand == "info":
        pblue_info()
    elif subcommand == "list":
        pblue_list()
    elif subcommand == "version":
        pblue_version();
    else:
        print "unrecognized subcommand %s"%(subcommand)
        usage();

    f = open(metadata_path, 'w+')
    pickle.dump(db, f)
    f.close()
        
